home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS05.ADF / IFF / iff.h < prev    next >
C/C++ Source or Header  |  1986-04-20  |  20KB  |  460 lines

  1.  
  2. #ifndef IFF_H
  3. #define IFF_H
  4. /*----------------------------------------------------------------------*/
  5. /* IFF.H  defs for IFF-85 Interchange Format Files.            1/22/86 */
  6. /*                                           */
  7. /* By Jerry Morrison and Steve Shaw, Electronic Arts.            */
  8. /* This software is in the public domain.                   */
  9. /*----------------------------------------------------------------------*/
  10.  
  11. #ifndef COMPILER_H
  12. #include "iff/compiler.h"
  13. #endif
  14.  
  15. #ifndef LIBRARIES_DOS_H
  16. #include "libraries/dos.h"
  17. #endif
  18.  
  19. #ifndef OFFSET_BEGINNING
  20. #define OFFSET_BEGINNING OFFSET_BEGINING
  21. #endif
  22.  
  23. typedef LONG IFFP;  /* Status code result from an IFF procedure */
  24.      /* LONG, because must be type compatable with ID for GetChunkHdr.*/
  25.      /* Note that the error codes below are not legal IDs.*/
  26. #define IFF_OKAY   0L    /* Keep going...*/
  27. #define END_MARK  -1L    /* As if there was a chunk at end of group.*/
  28. #define IFF_DONE  -2L    /* clientProc returns this when it has READ enough.
  29.                 * It means return thru all levels. File is Okay.*/
  30. #define DOS_ERROR -3L
  31. #define NOT_IFF   -4L    /* not an IFF file.*/
  32. #define NO_FILE   -5L    /* Tried to open file, DOS didn't find it.*/
  33. #define CLIENT_ERROR -6L /* Client made invalid request, for instance, write
  34.                 * a negative size chunk.*/
  35. #define BAD_FORM  -7L    /* A client read proc complains about FORM semantics;
  36.                 * e.g. valid IFF, but missing a required chunk.*/
  37. #define SHORT_CHUNK -8L  /* Client asked to IFFReadBytes more bytes than left
  38.                 * in the chunk. Could be client bug or bad form.*/
  39. #define BAD_IFF   -9L    /* mal-formed IFF file. [TBD] Expand this into a
  40.                 * range of error codes.*/
  41. #define LAST_ERROR BAD_IFF
  42.  
  43. /* This MACRO is used to RETURN immediately when a termination condition is
  44.  * found. This is a pretty weird macro. It requires the caller to declare a
  45.  * local "IFFP iffp" and assign it. This wouldn't work as a subroutine since
  46.  * it returns for it's caller. */
  47. #define CheckIFFP()   { if (iffp != IFF_OKAY) return(iffp); }
  48.  
  49.  
  50. /* ---------- ID -------------------------------------------------------*/
  51.  
  52. typedef LONG ID;    /* An ID is four printable ASCII chars but
  53.                 * stored as a LONG for efficient copy & compare.*/
  54.  
  55. /* Four-character IDentifier builder.*/
  56. #define MakeID(a,b,c,d)  ( (LONG)(a)<<24L | (LONG)(b)<<16L | (c)<<8 | (d) )
  57.  
  58. /* Standard group IDs.  A chunk with one of these IDs contains a
  59.    SubTypeID followed by zero or more chunks.*/
  60. #define FORM MakeID('F','O','R','M')
  61. #define PROP MakeID('P','R','O','P')
  62. #define LIST MakeID('L','I','S','T')
  63. #define CAT  MakeID('C','A','T',' ')
  64. #define FILLER MakeID(' ',' ',' ',' ')
  65. /* The IDs "FOR1".."FOR9", "LIS1".."LIS9", & "CAT1".."CAT9" are reserved
  66.  * for future standardization.*/
  67.  
  68. /* Pseudo-ID used internally by chunk reader and writer.*/
  69. #define NULL_CHUNK 0L           /* No current chunk.*/
  70.  
  71.  
  72. /* ---------- Chunk ----------------------------------------------------*/
  73.  
  74. /* All chunks start with a type ID and a count of the data bytes that 
  75.    follow--the chunk's "logicl size" or "data size". If that number is odd,
  76.    a 0 pad byte is written, too. */
  77. typedef struct {
  78.     ID      ckID;
  79.     LONG  ckSize;
  80.     } ChunkHeader;
  81.  
  82. typedef struct {
  83.     ID      ckID;
  84.     LONG  ckSize;
  85.     UBYTE ckData[ 1 /*REALLY: ckSize*/ ];
  86.     } Chunk;
  87.  
  88. /* Pass ckSize = szNotYetKnown to the writer to mean "compute the size".*/
  89. #define szNotYetKnown 0x80000001L
  90.  
  91. /* Need to know whether a value is odd so can word-align.*/
  92. #define IS_ODD(a)   ((a) & 1)
  93.  
  94. /* This macro rounds up to an even number. */
  95. #define WordAlign(size)   ((size+1)&~1)
  96.  
  97. /* ALL CHUNKS MUST BE PADDED TO EVEN NUMBER OF BYTES.
  98.  * ChunkPSize computes the total "physical size" of a padded chunk from
  99.  * its "data size" or "logical size". */
  100. #define ChunkPSize(dataSize)  (WordAlign(dataSize) + sizeof(ChunkHeader))
  101.  
  102. /* The Grouping chunks (LIST, FORM, PROP, & CAT) contain concatenations of
  103.  * chunks after a subtype ID that identifies the content chunks.
  104.  * "FORM type XXXX", "LIST of FORM type XXXX", "PROPerties associated
  105.  * with FORM type XXXX", or "conCATenation of XXXX".*/
  106. typedef struct {
  107.     ID      ckID;
  108.     LONG  ckSize;   /* this ckSize includes "grpSubID".*/
  109.     ID    grpSubID;
  110.     } GroupHeader;
  111.  
  112. typedef struct {
  113.     ID      ckID;
  114.     LONG  ckSize;
  115.     ID    grpSubID;
  116.     UBYTE grpData[ 1 /*REALLY: ckSize-sizeof(grpSubID)*/ ];
  117.     } GroupChunk;
  118.  
  119.  
  120. /* ---------- IFF Reader -----------------------------------------------*/
  121.  
  122. /******** Routines to support a stream-oriented IFF file reader *******
  123.  *
  124.  * These routines handle lots of details like error checking and skipping
  125.  * over padding. They're also careful not to read past any containing context.
  126.  *
  127.  * These routines ASSUME they're the only ones reading from the file.
  128.  * Client should check IFFP error codes. Don't press on after an error!
  129.  * These routines try to have no side effects in the error case, except
  130.  * partial I/O is sometimes unavoidable.
  131.  *
  132.  * All of these routines may return DOS_ERROR. In that case, ask DOS for the
  133.  * specific error code.
  134.  *
  135.  * The overall scheme for the low level chunk reader is to open a "group read
  136.  * context" with OpenRIFF or OpenRGroup, read the chunks with GetChunkHdr
  137.  * (and its kin) and IFFReadBytes, and close the context with CloseRGroup.
  138.  *
  139.  * The overall scheme for reading an IFF file is to use ReadIFF, ReadIList,
  140.  * and ReadICat to scan the file. See those procedures, ClientProc (below),
  141.  * and the skeleton IFF reader. */
  142.  
  143. /* Client passes ptrs to procedures of this type to ReadIFF which call them
  144.  * back to handle LISTs, FORMs, CATs, and PROPs.
  145.  *
  146.  * Use the GroupContext ptr when calling reader routines like GetChunkHdr.
  147.  * Look inside the GroupContext ptr for your ClientFrame ptr. You'll
  148.  * want to type cast it into a ptr to your containing struct to get your
  149.  * private contextual data (stacked property settings). See below. */
  150. #ifdef FDwAT
  151. typedef IFFP ClientProc(struct _GroupContext *);
  152. #else
  153. typedef IFFP ClientProc();
  154. #endif
  155.  
  156. /* Client's context for reading an IFF file or a group.
  157.  * Client should actually make this the first component of a larger struct
  158.  * (it's personal stack "frame") that has a field to store each "interesting"
  159.  * property encountered.
  160.  * Either initialize each such field to a global default or keep a boolean
  161.  * indicating if you've read a property chunk into that field.
  162.  * Your getList and getForm procs should allocate a new "frame" and copy the
  163.  * parent frame's contents. The getProp procedure should store into the frame
  164.  * allocated by getList for the containing LIST. */
  165. typedef struct _ClientFrame {
  166.    ClientProc *getList, *getProp, *getForm, *getCat;
  167.     /* client's own data follows; place to stack property settings */
  168.     } ClientFrame;
  169.  
  170. /* Our context for reading a group chunk. */
  171. typedef struct _GroupContext {
  172.     struct _GroupContext *parent; /* Containing group; NULL => whole file. */
  173.     ClientFrame *clientFrame;     /* Reader data & client's context state. */
  174.     BPTR file;      /* Byte-stream file handle. */
  175.     LONG position;  /* The context's logical file position. */
  176.     LONG bound;          /* File-absolute context bound
  177.                 * or szNotYetKnown (writer only). */
  178.     ChunkHeader ckHdr;   /* Current chunk header. ckHdr.ckSize = szNotYetKnown
  179.                 * means we need to go back and set the size (writer only).
  180.                 * See also Pseudo-IDs, above. */
  181.     ID subtype;          /* Group's subtype ID when reading. */
  182.     LONG bytesSoFar;     /* # bytes read/written of current chunk's data. */
  183.     } GroupContext;
  184.  
  185. /* Computes the number of bytes not yet read from the current chunk, given
  186.  * a group read context gc. */
  187. #define ChunkMoreBytes(gc)  ((gc)->ckHdr.ckSize - (gc)->bytesSoFar)
  188.  
  189.  
  190. /***** Low Level IFF Chunk Reader *****/
  191.  
  192. #ifdef FDwAT
  193.  
  194. /* Given an open file, open a read context spanning the whole file.
  195.  * This is normally only called by ReadIFF.
  196.  * This sets new->clientFrame = clientFrame.
  197.  * ASSUME context allocated by caller but not initialized.
  198.  * ASSUME caller doesn't deallocate the context before calling CloseRGroup.
  199.  * NOT_IFF ERROR if the file is too short for even a chunk header.*/
  200. extern IFFP OpenRIFF(BPTR, GroupContext *, ClientFrame *);
  201.               /*  file, new,            clientFrame  */
  202.  
  203. /* Open the remainder of the current chunk as a group read context.
  204.  * This will be called just after the group's subtype ID has been read
  205.  * (automatically by GetChunkHdr for LIST, FORM, PROP, and CAT) so the
  206.  * remainder is a sequence of chunks.
  207.  * This sets new->clientFrame = parent->clientFrame. The caller should repoint
  208.  * it at a new clientFrame if opening a LIST context so it'll have a "stack
  209.  * frame" to store PROPs for the LIST. (It's usually convenient to also
  210.  * allocate a new Frame when you encounter FORM of the right type.)
  211.  *
  212.  * ASSUME new context allocated by caller but not initialized.
  213.  * ASSUME caller doesn't deallocate the context or access the parent context
  214.  * before calling CloseRGroup.
  215.  * BAD_IFF ERROR if context end is odd or extends past parent. */
  216. extern IFFP OpenRGroup(GroupContext *, GroupContext *);
  217.              /*  parent,        nd-of-file.
  218.  *
  219.  * See also GetFChunkHdr, GetF1ChunkHdr, and GetPChunkHdr, below.*/
  220. extern ID       GetChunkHdr(GroupContext *);
  221.   /*  context.ckHdr.ckID    context  */
  222.  
  223. /* Read nBytes number of data bytes of current chunk. (Use OpenGroup, etc.
  224.  * instead to read the contents of a group chunk.) You can call this several
  225.  * times to read the data piecemeal.
  226.  * CLIENT_ERROR if nBytes < 0. SHORT_CHUNK if nBytes > ChunkMoreBytes(context)
  227.  * which could be due to a client bug or a chunk that's shorter than it
  228.  * ought to be (bad form). (on either CLIENT_ERROR or SHORT_CHUNK,
  229.  * IFFReadBytes won't read any bytes.) */
  230. extern IFFP IFFReadBytes(GroupContext *, BYTE *, LONG);
  231.                /*  context,        buffer, nBytes  */
  232.  
  233.  
  234. /***** IFF File Reader *****/
  235.  
  236. /* This is a noop ClientProc that you can use for a getList, getForm, getProp,
  237.  * or getCat procedure that just skips the group. A simple reader might just
  238.  * implement getForm, store ReadICat in the getCat field of clientFrame, and
  239.  * use SkipGroup for the getList and getProp procs.*/
  240. extern IFFP SkipGroup(GroupContext *);
  241.  
  242. /* IFF file reader.
  243.  * Given an open file, allocate a group context and use it to read the FORM,
  244.  * LIST, or CAT and it's contents. The idea is to parse the file's contents,
  245.  * and for each FORM, LIST, CAT, or PROP encountered, call the getForm,
  246.  * getList, getCat, or getProp procedure in clientFrame, passing the
  247.  * GroupContext ptr.
  248.  * This is achieved with the aid of ReadIList (which your getList should
  249.  * call) and ReadICat (which your getCat should call, if you don't just use
  250.  * ReadICat for your getCat). If you want to handle FORMs, LISTs, and CATs
  251.  * nested within FORMs, the getForm procedure must dispatch to getForm,
  252.  * getList, and getCat (it can use GetF1ChunkHdr to make this easy).
  253.  *
  254.  * Normal return is IFF_OKAY (if whole file scanned) or IFF_DONE (if a client
  255.  * proc said "done" first).
  256.  * See the skeletal getList, getForm, getCat, and getProp procedures. */
  257. extern IFFP ReadIFF(BPTR, ClientFrame *);
  258.                 /*  file, clientFrame  */
  259.  
  260. /* IFF LIST reader.
  261.  * Your "getList" procedure should allocate a ClientFrame, copy the parent's
  262.  * ClientFrame, and then call this procedure to do all the work.
  263.  *
  264.  * Normal return is IFF_OKAY (if whole LIST scanned) or IFF_DONE (if a client
  265.  * proc said "done" first).
  266.  * BAD_IFF ERROR if a PROP appears after a non-PROP. */
  267. extern IFFP ReadIList(GroupContext *, ClientFrame *);
  268.             /*  parent,         clientFrame  */
  269.  
  270. /* IFF CAT reader.
  271.  * Most clients can simply use this to read their CATs. If you must do extra
  272.  * setup work, put a ptr to your getCat procedure in the clientFrame, and
  273.  * have that procedure call ReadICat to do the detail work.
  274.  *
  275.  * Normal return is IFF_OKAY (if whole CAT scanned) or IFF_DONE (if a client
  276.  * proc said "done" first).
  277.  * BAD_IFF ERROR if a PROP appears in the CAT. */
  278. extern IFFP ReadICat(GroupContext *);
  279.            /*  parent  */
  280.  
  281. /* Call GetFChunkHdr instead of GetChunkHdr to read each chunk inside a FORM.
  282.  * It just calls GetChunkHdr and returns BAD_IFF if it gets a PROP chunk. */
  283. extern ID GetFChunkHdr(GroupContext *);
  284.   /*  context.ckHdr.ckID    context  */
  285.  
  286. /* GetF1ChunkHdr is like GetFChunkHdr, but it automatically dispatches to the
  287.  * getForm, getList, and getCat procedure (and returns the result) if it
  288.  * encounters a FORM, LIST, or CAT. */
  289. extern ID GetF1ChunkHdr(GroupContext *);
  290.   /*  context.ckHdr.ckID    context  */
  291.  
  292. /* Call GetPChunkHdr instead of GetChunkHdr to read each chunk inside a PROP.
  293.  * It just calls GetChunkHdr and returns BAD_IFF if it gets a group chunk. */
  294. extern ID GetPChunkHdr(GroupContext *);
  295.   /*  context.ckHdr.ckID    context  */
  296.  
  297. #else /* not FDwAT */
  298.  
  299. extern IFFP OpenRIFF();
  300. extern IFFP OpenRGroup();
  301. extern IFFP CloseRGroup();
  302. extern ID   GetChunkHdr();
  303. extern IFFP IFFReadBytes();
  304. extern IFFP SkipGroup();
  305. extern IFFP ReadIFF();
  306. extern IFFP ReadIList();
  307. extern IFFP ReadICat();
  308. extern ID   GetFChunkHdr();
  309. extern ID   GetF1ChunkHdr();
  310. extern ID   GetPChunkHdr();
  311.  
  312. #endif /* not FDwAT */
  313.  
  314. /* ---------- IFF Writer -----------------------------------------------*/
  315.  
  316. /******* Routines to support a stream-oriented IFF file writer *******
  317.  *
  318.  * These routines will random access back to set a chunk size value when the
  319.  * caller doesn't know it ahead of time. They'll also do things automatically
  320.  * like padding and error checking.
  321.  *
  322.  * These routines ASSUME they're the only ones writing to the file.
  323.  * Client should check IFFP error codes. Don't press on after an error!
  324.  * These routines try to have no side effects in the error case, except that
  325.  * partial I/O is sometimes unavoidable.
  326.  *
  327.  * All of these routines may return DOS_ERROR. In that case, ask DOS for the
  328.  * specific error code.
  329.  *
  330.  * The overall scheme is to open an output GroupContext via OpenWIFF or
  331.  * OpenWGroup, call either PutCk or {PutCkHdr {IFFWriteBytes}* PutCkEnd} for
  332.  * each chunk, then use CloseWGroup to close the GroupContext.
  333.  *
  334.  * To write a group (LIST, FORM, PROP, or CAT), call StartWGroup, write out
  335.  * its chunks, then call EndWGroup. StartWGroup automatically writes the
  336.  * group header and opens a nested context for writing the contents.
  337.  * EndWGroup closes the nested context and completes the group chunk. */
  338.  
  339.  
  340. #ifdef FDwAT
  341.  
  342. /* Given a file open for output, open a write context.
  343.  * The "limit" arg imposes a fence or upper limit on the logical file
  344.  * position for writing data in this context. Pass in szNotYetKnown to be
  345.  * bounded only by disk capacity.
  346.  * ASSUME new context structure allocated by caller but not initialized.
  347.  * ASSUME caller doesn't deallocate the context before calling CloseWGroup.
  348.  * The caller is only allowed to write out one FORM, LIST, or CAT in this top
  349.  * level context (see StartWGroup and PutCkHdr).
  350.  * CLIENT_ERROR if limit is odd.*/
  351. extern IFFP OpenWIFF(BPTR, GroupContext *, LONG);
  352.            /*  file, new,            limit {file position}  */
  353.  
  354. /* Start writing a group (presumably LIST, FORM, PROP, or CAT), opening a
  355.  * nested context. The groupSize includes all nested chunks + the subtype ID.
  356.  *
  357.  * The subtype of a LIST or CAT is a hint at the contents' FORM type(s). Pass
  358.  * in FILLER if it's a mixture of different kinds.
  359.  *
  360.  * This writes the chunk header via PutCkHdr, writes the subtype ID via
  361.  * IFFWriteBytes, and calls OpenWGroup. The caller may then write the nested
  362.  * chunks and finish by calling EndWGroup.
  363.  * The OpenWGroup call sets new->clientFrame = parent->clientFrame.
  364.  *
  365.  * ASSUME new context structure allocated by caller but not initialized.
  366.  * ASSUME caller doesn't deallocate the context or access the parent context
  367.  * before calling CloseWGroup.
  368.  * ERROR conditions: See PutCkHdr, IFFWriteBytes, OpenWGroup. */
  369. extern IFFP StartWGroup(GroupContext *, ID, LONG, ID, GroupContext *);
  370.               /*  parent, groupType, groupSize, subtype, new  */
  371.  
  372. /* End a group started by StartWGroup.
  373.  * This just calls CloseWGroup and PutCkEnd.
  374.  * ERROR conditions: See CloseWGroup and PutCkEnd. */
  375. extern IFFP EndWGroup(GroupContext *);
  376.               /*  old  */
  377.  
  378. /* Open the remainder of the current chunk as a group write context.
  379.  * This is normally only called by StartWGroup.
  380.  *
  381.  * Any fixed limit to this group chunk or a containing context will impose
  382.  * a limit on the new context.
  383.  * This will be called just after the group's subtype ID has been written
  384.  * so the remaining contents will be a sequence of chunks.
  385.  * This sets new->clientFrame = parent->clientFrame.
  386.  * ASSUME new context structure allocated by caller but not initialized.
  387.  * ASSUME caller doesn't deallocate the context or access the parent context
  388.  * before calling CloseWGroup.
  389.  * CLIENT_ERROR if context end is odd or PutCkHdr wasn't called first. */
  390. extern IFFP OpenWGroup(GroupContext *, GroupContext *);
  391.              /*  parent,         new  */
  392.  
  393. /* Close a write context and update its parent context.
  394.  * This is normally only called by EndWGroup.
  395.  *
  396.  * If this is a top level context (created by OpenWIFF) we'll set the file's
  397.  * EOF (end of file) but won't close the file.
  398.  * After calling this, the old context may be deallocated and the parent
  399.  * context can be accessed again.
  400.  *
  401.  * Amiga DOS Note: There's no call to set the EOF. We just position to the
  402.  * desired end and return. Caller must Close file at that position.
  403.  * CLIENT_ERROR if PutCkEnd wasn't called first. */
  404. extern IFFP CloseWGroup(GroupContext *);
  405.               /*  old  */
  406.  
  407. /* Write a whole chunk to a GroupContext. This writes a chunk header, ckSize
  408.  * data bytes, and (if needed) a pad byte. It also updates the GroupContext.
  409.  * CLIENT_ERROR if ckSize == szNotYetKnown. See also PutCkHdr errors. */
  410. extern IFFP PutCk(GroupContext *, ID,   LONG,   BYTE *);
  411.            /*  context,        ckID, ckSize, *data  */
  412.  
  413. /* Write just a chunk header. Follow this will any number of calls to
  414.  * IFFWriteBytes and finish with PutCkEnd.
  415.  * If you don't yet know how big the chunk is, pass in ckSize = szNotYetKnown,
  416.  * then PutCkEnd will set the ckSize for you later.
  417.  * Otherwise, IFFWriteBytes and PutCkEnd will ensure that the specified
  418.  * number of bytes get written.
  419.  * CLIENT_ERROR if the chunk would overflow the GroupContext's bound, if
  420.  * PutCkHdr was previously called without a matching PutCkEnd, if ckSize < 0
  421.  * (except szNotYetKnown), if you're trying to write something other
  422.  * than one FORM, LIST, or CAT in a top level (file level) context, or
  423.  * if ckID <= 0 (these illegal ID values are used for error codes). */
  424. extern IFFP PutCkHdr(GroupContext *, ID,   LONG);
  425.            /*  context,        ckID, ckSize  */
  426.  
  427. /* Write nBytes number of data bytes for the current chunk and update
  428.  * GroupContext.
  429.  * CLIENT_ERROR if this would overflow the GroupContext's limit or the
  430.  * current chunk's ckSize, or if PutCkHdr wasn't called first, or if
  431.  * nBytes < 0. */
  432. extern IFFP IFFWriteBytes(GroupContext *, BYTE *, LONG);
  433.                 /*  context,        *data,  nBytes  */
  434.  
  435. /* Complete the current chunk, write a pad byte if needed, and update
  436.  * GroupContext.
  437.  * If current chunk's ckSize = szNotYetKnown, this goes back and sets the
  438.  * ckSize in the file.
  439.  * CLIENT_ERROR if PutCkHdr wasn't called first, or if client hasn't
  440.  * written 'ckSize' number of bytes with IFFWriteBytes. */
  441. extern IFFP PutCkEnd(GroupContext *);
  442.            /*  context  */
  443.  
  444. #else /* not FDwAT */
  445.  
  446. extern IFFP OpenWIFF();
  447. extern IFFP StartWGroup();
  448. extern IFFP EndWGroup();
  449. extern IFFP OpenWGroup();
  450. extern IFFP CloseWGroup();
  451. extern IFFP PutCk();
  452. extern IFFP PutCkHdr();
  453. extern IFFP IFFWriteBytes();
  454. extern IFFP PutCkEnd();
  455.  
  456. #endif /* not FDwAT */
  457.  
  458. #endif IFF_H
  459.  
  460.